package sky;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class InitConnection {
    Connection connection=null;
    final static Logger logger = Logger.getLogger(InitConnection.class);
    public void InitDB() {
        BasicConfigurator.configure();
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("C://Project/Config.properties"));
        } catch (IOException e) {
            logger.info("Ошибка подключения файла конфигураций "+e);
        }
        try {
            Class.forName("org.hsqldb.jdbcDriver");
        } catch(ClassNotFoundException e) {
            logger.info("Ошибка загрузки драйвера "+e);
        }
        try {
            connection = DriverManager.getConnection(properties.getProperty("HsqlConnection"));
        } catch(SQLException e) {
            logger.info("Ошибка установления соединения "+e);
        }
        try {
            Statement statement = connection.createStatement();
            for (int i=1; i<16; i++) {
                statement.executeUpdate(properties.getProperty("SqlCreate"+i));
            }
            //Flight flight = DAOFactory.getDAOFactory(1).getFlightDAO().showInformation(1);
            //System.out.println(flight.getDepartureAirport());
            //ResultSet resultSet = statement.executeQuery("SELECT * FROM Flights");
            //while (resultSet.next()) {
            //    System.out.println(resultSet.getString("DepartureAirport"));
            //}
        } catch (SQLException e) {
            logger.info("Ошибка выполнения SQL-запроса "+ e);
        }
    }
}
